package front;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.lang.InterruptedException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.net.ConnectException;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import java.awt.Dialog.ModalityType;
import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneLayout;
import javax.swing.JPopupMenu;
import javax.swing.AbstractAction;

import servidores.Libro;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.Point;


public class Servicio {
	private static final String ipServerCatalogo = "localhost";
	private static final String ipServerPedidos = "localhost";
	private static final int puertoCatalogo = 1234;
	private static final int puertoPedidos = 1234;
    private static ObjectInputStream flujoEntradaCatalogo;
    private static ObjectOutputStream flujoSalidaCatalogo;
    private static ObjectInputStream flujoEntradaPedidos;
    private static ObjectOutputStream flujoSalidaPedidos;
    private static Socket socketCatalogo;
    private static Socket socketPedidos;
    private static int ts;

    private static Servicio INSTANCIA = null;
    
    private synchronized static void crearInstancia() throws UnknownHostException, IOException {
        if (INSTANCIA == null) { 
            INSTANCIA = new Servicio();
            System.out.println("INSTANCIANDO SERVICIO");
        }
    }
 
    public static Servicio getInstance() throws UnknownHostException, IOException {
        if (INSTANCIA == null) crearInstancia();
        return INSTANCIA;
    }

    private Servicio() throws UnknownHostException, IOException  {
        	this.socketCatalogo = new Socket(ipServerCatalogo,puertoCatalogo);
//          socketPedidos = new Socket(ipServerPedidos,puertoPedidos);
            System.out.println("CONECTADO AL HOST");
            this.flujoSalidaCatalogo = new ObjectOutputStream(socketCatalogo.getOutputStream());
            this.flujoEntradaCatalogo = new ObjectInputStream(socketCatalogo.getInputStream());
//          flujoEntradaPedidos= new ObjectInputStream(new BufferedInputStream(socketPedidos.getInputStream()));
//          flujoSalidaPedidos = new ObjectOutputStream(new BufferedOutputStream(socketPedidos.getOutputStream()));
    }     
    
    private Object recieveFrom(ObjectInputStream flujo) throws IOException, ClassNotFoundException {
		Object msg = flujo.readObject();
        System.out.println("MENSAJE RECIBIDO!!!!!!!!!");
        return msg;
    }
    
    private void sendTo(ObjectOutputStream flujo, String mensaje) throws IOException {
        flujo.writeUTF(mensaje);
        flujo.flush();
    }
    
    private void desconectar() throws IOException {
        flujoEntradaCatalogo.close();
        flujoSalidaCatalogo.close();
//        flujoEntradaPedidos.close();
//        flujoSalidaPedidos.close();
//        socketPedidos.close();
        socketCatalogo.close();
    }

	public synchronized List<String> getListaTemas() throws IOException, ClassNotFoundException {
        sendTo(flujoSalidaCatalogo,"listaTemas");
        List<String> msg = (List<String>) recieveFrom(flujoEntradaCatalogo);
		return msg;
	}

	public synchronized List<Libro> buscarPorTituloTema() throws IOException, ClassNotFoundException {
		return null;
	}

	public synchronized List<Libro> buscarPorTema() throws IOException, ClassNotFoundException {
		return null;
	}

	public synchronized List<Libro> buscarPorTitulo() throws IOException, ClassNotFoundException {
		return null;
	}

	public static int getTs() {
		return ts;
	}

	public static void setTs(int ts) {
		Servicio.ts = ts;
	}
    
}



